<?php
/**
 * 
 *
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Jürgen A.Lamers <jaloma.ac@googlemail.com>
 */
 
if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');
 
/**
 * All DokuWiki plugins to extend the parser/rendering mechanism
 * need to inherit from this class
 */
class syntax_plugin_jalbum_jalbum extends DokuWiki_Syntax_Plugin {

  /**
   * default parameters of the jalbum tag
   */
  var $dflt = array(
		    'width' => 20,
		    'height' => 20,
		    'align' => 'top',
			'albumid' => 'off',
		    'albumdir' => 'off',
		    'albumtitle' => 'off',
		    'albumshow' => 'off',
			'jpgtohtml' => 'on'
		    );
  
  /**
   * return some info
   */
  function getInfo(){
    return array(
		 'author' => 'Juergen A.Lamers',
		 'email'  => 'jaloma.ac@googlemail.com',
		 'date'   => @file_get_contents(DOKU_PLUGIN . 'jalbum/VERSION'),
		 'name'   => 'jalbum',
		 'desc'   => 'Einfacher Zugriff auf Alben und Thumbs von JAlbum',
		 'url'    => 'http://wiki.dokuwiki.org/plugin:jalbum'
		 );
  }
 
  /**
   * What kind of syntax are we?
   */
  function getType(){
    return 'substition';
  }
  
  function getSort(){ return 305; }
  
  function connectTo($mode) {
    $this->Lexer->addSpecialPattern('<jalbum ?[^>\n]*>.*?</jalbum>',$mode,'plugin_jalbum_jalbum');
  }
 
 
  /**
   * Handle the match
   */
  function handle($match, $state, $pos, &$handler){
    list($str_params,$str_images) = explode('>',substr($match,7,-9),2);
    $thumb = $this->_extract_params($str_params);
    $imgs = $this->_extract_images($str_images);
    return array($thumb,$imgs);
  }
 
  /**
   * Create output
   */
  function render($mode, &$renderer, $data) {
    global $conf;
    if ($mode == 'xhtml') {
      list($param,$imgs) = $data;
      if (isset($param['albumid']) && $param['albumid'] != 'off') {
      	$txt = '';
		$url = 'http://jalbum.net/browse/user/album/'.$param['albumid'];
	    $target = "target='extern' ";
		$fullview .= '<a '.$target.'href="'.$url.'">';
		$fullview .= $param['albumtitle'];
		$fullview .= '</a>';
      	if (isset($param['albumshow']) && $param['albumshow'] != 'off') {
      		$width = $param['width'];
			if ($width == 20) {
				$width = 800;
			}
			$height = $param['height'];
			if ($height == 20) {
				$height = '640';
			}
			$txt .= '<div class="jalbumnet">';
			$txt .= '<iframe align="left" valign="top" title="'.$param['albumtitle'].'"'.
			' src="'.$url.'"'.
			' width="'.$width.'"'.
			' height="'.$height.'"'.
			' style="width:'.$param['width'].'; height: '.$param['height'].'"'.
			'>'.
			'</iframe>'	;
			$txt .= '<br/>Fullview: '.$fullview;
			$txt .= '</div>';			
   		} else 	{
			$txt .= $fullview;
		}
		$renderer->doc .= $txt;
      } elseif (isset($param['albumdir']) && $param['albumdir'] != 'off') {
		$url = $this->getConf('baseurl').$param['albumdir'];
		if (isset($this->getConf['target'])) {
	  		$target = 'target="'.$this->getConf['target'].'" ';
		} else {
	  		$target = '';
		}
		$txt .= '<a '.$target.'href="'.$url.'">';
		$txt .= $param['albumtitle'];
		$txt .= '</a>';
		$renderer->doc .= $txt;
      } else {
		foreach($imgs as $img) {
	  		list($image_path,$image_name,$alttxt) = $img;
	  //	  $img_href = str_ireplace('jpg','html',$image_name);
	  		$alttxt = strip_tags($alttxt);
	  		$img_href = str_replace('JPG','html',$image_name);
	  		$txt = "";
	  		if (isset($this->getConf['target'])) {
	    		$target = 'target="'.$this->getConf['target'].'" ';
	  		} else {
	    		$target = '';
	  		}
			if ($param['jpgtohtml'] == 'on') {
	  			$txt .= '<a '.$target.' href="'.$this->getConf('baseurl').$image_path.'/slides/'.$img_href.'">';
			}
	  		$txt .= '<img border="0" src="'.$this->getConf('baseurl').$image_path.'/thumbs/'.$image_name.'" alt="'.$alttxt.'"';
	  		if (isset($param['width'])) {
	    		$txt .= ' width="'.$param['width'].'"';
	  		}
	  		if (isset($param['align'])) {
	    		$txt .= ' align="'.$param['align'].'"';
	  		} else {
	    		$txt .= ' align="'.$this->dflt['align'].'"';
	  		}
	  		if (isset($param['height'])) {
	    		$txt .= ' height="'.$param['height'].'"';
	  		} else {
	    		$txt .= ' height="'.$this->dflt['height'].'"';
	  		}
	  		$txt .= '/>';//IMG
			if ($param['jpgtohtml'] == 'on') {
	  			$txt .= '</a>';
			}
	  		$renderer->doc .= $txt;
		}
      }
      return true;
    }
    return false;
  }
 
  /**
   * extract parameters for jalbum from the parameter string
   *
   * @param   string    $str_params   string of key="value" or key='value' pairs
   * @return  array                   associative array of parameters key=>value
   */
  function _extract_params($str_params) {
    $param = array();
    preg_match_all('/(\w*)="(.*?)"/us',$str_params,$param,PREG_SET_ORDER);
    if (sizeof($param) == 0) {
      preg_match_all("/(\w*)='(.*?)'/us",$str_params,$param,PREG_SET_ORDER);
    }
    // parse match for instructions, break into key value pairs      
    $thumb = $this->dflt;
    foreach($param as $kvpair) {
      list($match,$key,$val) = $kvpair;
//      $key = strtolower($key);
      if (isset($thumb[$key])) $thumb[$key] = $val;        
    }
    return $thumb;
  } 


  /**
   * extract images from the wiki syntax data
   *
   * @param   string    $str_images   multi-line string of imagepath,imagename,description triplets
   * @return  array                   multi-dimensional array of imagepath,imagename,description triplets
   */
  function _extract_images($str_images) {
    $image = array();
    preg_match_all('/^(.*),(.*),(.*?)$/um',$str_images,$image,PREG_SET_ORDER);
    
    $overlay = array();
    foreach ($image as $pt) {
      list($match,$imgpath,$img,$text) = $pt;
      
      $text = addslashes(str_replace("\n","",p_render("xhtml",p_get_instructions($text),$info)));
      
      $overlay[] = array($imgpath,$img,$text);
    }
    return $overlay;
  }
} // class
